import { Request, Response } from 'express';
import { fetchLeetCodeStats } from '../services/leetcode.service';
import { getLeetCodeUserData, updateLeetCodeData } from '../services/mongodb.service';
import { ILeetCodeUser } from '../types';
import { generateCard, CardType, getThemeConfig } from '../services/svg.service';
import { activeTheme } from '../config/theme.config';

// 获取LeetCode统计
export const getLeetCodeStats = async (req: Request, res: Response): Promise<void> => {
  try {
    const username = req.params.username;
    // 从查询参数获取主题名称，支持主题参数
    const themeName = req.query.theme as string;
    // 获取主题配置
    const themeConfig = getThemeConfig(themeName);
    // 获取区域参数，默认为非中国区
    const useCN = req.query.cn === 'true';
    // 获取缓存时间
    const cacheTimeInSeconds = req.query.cacheSeconds ? parseInt(req.query.cacheSeconds as string) : 120;
    console.debug(`处理LeetCode请求: 用户名=${username}, 区域=${useCN ? 'CN' : 'US'}`);


    if (!username) {
      res.status(400).set('Content-Type', 'image/svg+xml').send(generateCard(CardType.ERROR, '未提供用户名', themeConfig));
      return;
    }

    // 从缓存/数据库获取用户数据
    const { data, needsFetch } = await getLeetCodeUserData(username, cacheTimeInSeconds);

    // 确保当请求的区域与存储的数据区域不一致时，始终从API重新获取数据
    const regionMismatch = data && ((useCN && data.region !== 'CN') || (!useCN && data.region === 'CN'));
    if (!data || regionMismatch || needsFetch) {
      // 从LeetCode API获取数据，传入区域参数
      const result = await fetchLeetCodeStats(username, useCN);

      if (result.success && result.data) {
        // 将数据存入数据库/缓存
        const leetcodeData: ILeetCodeUser = {
          username: result.data.username,
          totalSolved: result.data.totalSolved,
          easySolved: result.data.easySolved,
          mediumSolved: result.data.mediumSolved,
          hardSolved: result.data.hardSolved,
          acceptanceRate: result.data.acceptanceRate,
          lastUpdated: result.data.lastUpdated,
          region: result.data.region,  // 保存区域信息
          expireAt: new Date(Date.now() + cacheTimeInSeconds * 1000),  // 设置过期时间
        };

        await updateLeetCodeData(username, leetcodeData, cacheTimeInSeconds);

        // 返回SVG
        res.set('Content-Type', 'image/svg+xml');
        res.set('Cache-Control', 'max-age=1800'); // 30分钟缓存
        res.send(generateCard(CardType.LEETCODE, leetcodeData, themeConfig));
        return;
      }
    }

    // 如果已有缓存数据或无法获取新数据，使用缓存数据
    res.set('Content-Type', 'image/svg+xml');
    res.set('Cache-Control', 'max-age=1800'); // 30分钟缓存
    res.send(generateCard(CardType.LEETCODE, data, themeConfig));

  } catch (error: any) {
    console.error(`LeetCode控制器错误: ${error.message}`);
    // 从查询参数获取主题名称
    const themeName = req.query.theme as string;
    // 获取主题配置
    const themeConfig = getThemeConfig(themeName);
    res.set('Content-Type', 'image/svg+xml');
    res.status(500).send(generateCard(CardType.ERROR, `处理请求时出错: ${error.message}`, themeConfig));
  }
};
